/**
 * 埋点日志 MongoDB Schema
 */

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type TrackingLogDocument = TrackingLog & Document;

@Schema({ collection: 'tracking_logs', timestamps: true })
export class TrackingLog {
  @Prop({ required: true, index: true })
  trackingId: string;

  @Prop({ required: true })
  websiteId: number;

  @Prop({ required: true })
  url: string;

  @Prop()
  title?: string;

  @Prop()
  referrer?: string;

  @Prop({ required: true, index: true })
  ip: string;

  @Prop()
  userAgent?: string;

  @Prop({ index: true })
  sessionId?: string;

  @Prop({ type: Object })
  customData?: Record<string, any>;

  @Prop({ type: Object })
  deviceInfo?: {
    browser?: string;
    os?: string;
    device?: string;
  };

  @Prop({ type: Object })
  locationInfo?: {
    country?: string;
    region?: string;
    city?: string;
  };

  @Prop({ default: Date.now, index: true })
  timestamp: Date;
}

export const TrackingLogSchema = SchemaFactory.createForClass(TrackingLog);

// 创建索引
TrackingLogSchema.index({ trackingId: 1, timestamp: -1 });
TrackingLogSchema.index({ websiteId: 1, timestamp: -1 });
TrackingLogSchema.index({ sessionId: 1 });
TrackingLogSchema.index({ ip: 1, timestamp: -1 });

